package com.shop.admin.dao.pink;

import com.shop.jpa.BaseDao;
import com.shop.domain.pink.StoreSnapPink;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Repository
public interface StoreSnapPinkDao extends BaseDao<StoreSnapPink, Long> {


    //中奖修改
    @Modifying(clearAutomatically = true)
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Query(nativeQuery = true,value = "update t_store_snap_pink set pink_status =1,zj_time=now() where id in ?1")
    int updatePinkFives(List<Long> fiveIds);

    //中奖人数
    @Query(nativeQuery = true,value = "select p.* from t_user_snap_pink_info i inner join t_store_snap_pink p on p.uid=i.uid where i.pink_id=?1  and p.is_deleted=0   and  p.`status`=1 and p.pink_status=0 order by rand() desc limit ?2")
    List<StoreSnapPink> findAllPinkWnning(Long pink_id,int limit);

    //查询拼团用户总数
    @Query(nativeQuery = true,value = "select count(*) from  t_store_snap_pink   where pink_id=?1  and is_deleted=0   and  `status`=1 and pink_status=0 ")
    Integer getAllPinkCount(Long pink_id);

    //未中奖修改
    @Modifying(clearAutomatically = true)
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Query(nativeQuery = true,value = "update t_store_snap_pink set pink_status =2 where pink_id = ?1 and pink_status=0 and status =1 and is_deleted = 0")
    int updateNotPinkStatus(Long pink_id);

    //修改拼中用户金额 及订单状态,订单修改未可以结算状态
    @Modifying(clearAutomatically = true)
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Query(nativeQuery = true,value = "update t_store_snap_pink p set p.pink_status = 3,p.status=2,p.rebate_status=1 where p.pink_status=1 and p.is_deleted=0 and p.pink_id=?1 and p.status=1")
    int updatePinkSuccessAddPinkNum(Long pink_id);


    //修改未拼中用户金额 及订单状态
    @Modifying(clearAutomatically = true)
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    @Query(nativeQuery = true,value = "update  t_store_snap_pink p  " +
            "set p.pink_status=4 ,p.status = 7 " +
            "where p.pink_status=2 and p.is_deleted=0 and p.pink_id=?1 and p.status=1 ")
    int updateNotPzSnapPink(Long pink_id);

    // 设置返佣状态
    @Modifying(clearAutomatically = true)
    @Query(nativeQuery = true,value = "update t_store_snap_pink set rebate_status = ?2 where id = ?1 and rebate_status = ?3")
    int updateRebateStatus(Long id, int status, int lastStatus);

    //查询待结算的拼团订单
    @Query(nativeQuery = true,value = "select p.* from t_store_snap_pink p where p.rebate_status = 1 order by id asc limit ?1")
    List<StoreSnapPink> findNeedSettlement(int limit);

    @Query("select count(*) from StoreSnapPink where status = 1 and uid = ?1 and pinkId = ?2")
    int countPinking(Long userId, Long pinkId);

    @Query(nativeQuery = true,value = "select p.* from t_store_snap_pink p where `pay_code` > 0 and `status` = 0 and `create_time` > ?1 and `create_time` < ?3 order by id asc limit ?2")
    List<StoreSnapPink> findNeedPayList(Date beginTime, int limit, Date endTime);
}
